Class('UIDesigner', 'linb.Com', {
    Instance: {
        $classEditor: null,
        $pageviewType: 'linb.UI.Tabs',
        $firstView: "design",
        $dftCodePath: '../Scripts/Linb/VisualJS/template/',
        $dftCodeFileName: 'index.js',
        $fetchedCode: '',
        $iniCode: '',
        $url: '',
        $dirty: false,
        onDestroy: function() {
            this.$classEditor.destroy();
        },
        events: {
            onReady: function() {
                SPA = this;
                linb.ComFactory.setProfile(CONF.ComFactoryProfile);
            },
            onRender: function(com, threadid) {
                com.setValue(com.$fetchedCode || com.$iniCode, com.$fetchedurl);
                com.$classEditor.setDftPage(com.$firstView);
            }
        },
        iniResource: function(com, threadid) {
            //Load default code(insert to the current thread)
            var com = this,
                url = _.urlDecode(location.href.split('#')[1], 'url'),
                hash = {};
            var surl = "";
            if (FormAction == "modify")
                surl = FormSource;
            else
                surl = com.$dftCodePath + /* linb.getLang()+"/"+ */com.$dftCodeFileName;
            hash.ajax1 = linb.Ajax(surl, '', function(code) {
                com.$iniCode = code.replace('{className}', 'App');
            }, function() {
                hash.ajax1 = linb.Ajax(com.$dftCodePath + com.$dftCodeFileName, function(code) {
                    com.$iniCode = code.replace('{className}', 'App');
                }, function() { }, null, { rspType: "text" });
                //alert(linb.getRes('VisualJS.builder.noexist', com.$dftCodePath));
            }, null, { rspType: "text" });

            if (url) {
                com.$url = url;
                hash.ajax2 = linb.Ajax(CONF.phpPath, {
                    key: CONF.requestKey,
                    para: {
                        action: 'fetchwebfile',
                        path: url
                    }
                }, function(txt) {
                    var obj = txt;
                    if (!obj.error) {
                        com.$fetchedCode = obj.data;
                        com.$fetchedurl = url;
                    } else
                        linb.message(obj.error.message);
                }, function() {
                    alert(linb.getRes('VisualJS.builder.noexist', url));
                });
            }
            linb.absIO.groupCall(hash, null, null, null, threadid);
        },
        iniExComs: function(com, threadid) {
            var com = this;
            //New an instance of VisualJS.JSEditor

            linb.ComFactory.newCom('VisualJS.JSEditor', function(threadid) {
                var inn = this;
                inn.host = com;
                inn.setEvents('onValueChanged', function() {
                    com.$dirty = true;
                });
                //inn.setDisplay("none");
                inn.create(function(o, threadid) {
                    inn.paneMain.setDockMargin({ "left": 0, "top": 0, "right": 0, "bottom": 0 });
                    //Replace the Tag one
                    com.appRoot.append(inn);
                }, threadid);

                com.$classEditor = inn;
            }, threadid, { $pageviewType: com.$pageviewType }
            );
        },
        getValue: function() {
            return this.$classEditor.getValue();
        },
        setValue: function(str, url) {
            var self = this;
            if (str)
                self.$classEditor.setValue(str);
            if (url)
                self.$url = url;
            var dis = self.$url ? !linb.absIO.isCrossDomain(self.$url) ? '' : 'none' : 'none';

            self.$dirty = false;
        },

        iniComponents: function() {
            // [[code created by jsLinb UI Builder
            var host = this, children = [], append = function(child) { children.push(child.get(0)) };
            append(
                (new linb.UI.Pane)
                .setHost(host, "appRoot")
                .setDock("fill")
                .setWidth(800)
                .setHeight(600)
                .setDockMinW(800)
                .setDockMinH(400)
            );
            return children;
        }
    }
});